{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## This notebook flatten the diffraction patterns and the probe profile into (chn, 256, 256) for training and testing, where chn is depdendent on R"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "data_folder_prefix = 'data/'\n",
    "prefix = data_folder_prefix + 'Simulated/R_Sweep/'\n",
    "photon_level = 1e4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "R = [0.25, 0.5, 1, 2]\n",
    "chns = [5, 3, 2, 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for idx, R in enumerate(R):\n",
    "    \n",
    "    tr_patterns = np.load(prefix + 'tr_patterns-R-%0.2f-phperpix-%d.npy' % (R, photon_level))[:, 1:-1, 1:-1]\n",
    "    test_patterns = np.load(prefix + 'test_patterns-R-%0.2f-phperpix-%d.npy' % (R, photon_level))[:, 1:-1, 1:-1]\n",
    "    probe = np.load(prefix + 'probe-R-%0.2f.npy' % R)[:, 1:-1, 1:-1]\n",
    "    \n",
    "    if R == 2:\n",
    "        tr_patterns = np.pad(tr_patterns, ((0, 0), (64, 64), (64, 64)))\n",
    "        test_patterns = np.pad(test_patterns, ((0, 0), (64, 64), (64, 64)))\n",
    "        probe = np.pad(probe, ((64, 64), (64, 64)))\n",
    "    \n",
    "    chn = int(chns[idx])\n",
    "    \n",
    "    flatten = np.zeros((4000, 256, 256, chn**2))\n",
    "\n",
    "    for i in range(4000):\n",
    "        for j in range(chn):\n",
    "            for k in range(chn):\n",
    "                flatten[i, :, :, chn*j + k] = tr_patterns[i, 256*j:256*(j+1), 256*k:256*(k+1)]\n",
    "\n",
    "    np.save(prefix + 'tr_patterns-flatten-R-%0.2f-phperpix-%d.npy' % (R, photon_level), flatten)\n",
    "    \n",
    "    \n",
    "\n",
    "    flatten = np.zeros((100, 256, 256, chn**2))\n",
    "\n",
    "    for i in range(100):\n",
    "        for j in range(chn):\n",
    "            for k in range(chn):\n",
    "                flatten[i, :, :, chn*j + k] = test_patterns[i, 256*j:256*(j+1), 256*k:256*(k+1)]\n",
    "\n",
    "    np.save(prefix + 'test_patterns-flatten-R-%0.2f-phperpix-%d.npy' % (R, photon_level), flatten)\n",
    "    \n",
    "\n",
    "    flatten = np.zeros((256, 256, 2*chn**2))\n",
    "\n",
    "    for j in range(chn):\n",
    "        for k in range(chn):\n",
    "            print(chn*j + k)\n",
    "            flatten[:, :, chn*j + k] = np.abs(probe[256*j:256*(j+1), 256*k:256*(k+1)])\n",
    "            flatten[:, :, chn**2 + chn*j + k] = np.angle(probe[256*j:256*(j+1), 256*k:256*(k+1)])\n",
    "\n",
    "    np.save(prefix + 'probe-flatten-R-%0.2f.npy' % R, flatten)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "R = 0.5\n",
    "chns = 3\n",
    "photon_levels = [[1e-2], [1e-1], [1], [1e1], [1e2], [1e3]]\n",
    "\n",
    "data_folder_prefix = 'data/'\n",
    "prefix = data_folder_prefix + 'Simulated/Fixed_R_Noise_Sweep/'\n",
    "\n",
    "for photon_level in photon_levels:\n",
    "    \n",
    "    tr_patterns = np.load(prefix + 'tr_patterns-R-%0.2f-phperpix-%d.npy' % (R, photon_level))[:, 1:-1, 1:-1]\n",
    "    test_patterns = np.load(prefix + 'test_patterns-R-%0.2f-phperpix-%d.npy' % (R, photon_level))[:, 1:-1, 1:-1]\n",
    "    probe = np.load(prefix + 'probe-R-%0.2f.npy' % R)[:, 1:-1, 1:-1]\n",
    "    \n",
    "    if R == 2:\n",
    "        tr_patterns = np.pad(tr_patterns, ((0, 0), (64, 64), (64, 64)))\n",
    "        test_patterns = np.pad(test_patterns, ((0, 0), (64, 64), (64, 64)))\n",
    "        probe = np.pad(probe, ((64, 64), (64, 64)))\n",
    "    \n",
    "    chn = int(chns[idx])\n",
    "    \n",
    "    flatten = np.zeros((4000, 256, 256, chn**2))\n",
    "\n",
    "    for i in range(4000):\n",
    "        for j in range(chn):\n",
    "            for k in range(chn):\n",
    "                flatten[i, :, :, chn*j + k] = tr_patterns[i, 256*j:256*(j+1), 256*k:256*(k+1)]\n",
    "\n",
    "    np.save(prefix + 'tr_patterns-flatten-R-%0.2f-phperpix-%d.npy' % (R, photon_level), flatten)\n",
    "    \n",
    "    \n",
    "\n",
    "    flatten = np.zeros((100, 256, 256, chn**2))\n",
    "\n",
    "    for i in range(100):\n",
    "        for j in range(chn):\n",
    "            for k in range(chn):\n",
    "                flatten[i, :, :, chn*j + k] = test_patterns[i, 256*j:256*(j+1), 256*k:256*(k+1)]\n",
    "\n",
    "    np.save(prefix + 'test_patterns-flatten-R-%0.2f-phperpix-%d.npy' % (R, photon_level), flatten)\n",
    "    \n",
    "\n",
    "    flatten = np.zeros((256, 256, 2*chn**2))\n",
    "\n",
    "    for j in range(chn):\n",
    "        for k in range(chn):\n",
    "            print(chn*j + k)\n",
    "            flatten[:, :, chn*j + k] = np.abs(probe[256*j:256*(j+1), 256*k:256*(k+1)])\n",
    "            flatten[:, :, chn**2 + chn*j + k] = np.angle(probe[256*j:256*(j+1), 256*k:256*(k+1)])\n",
    "\n",
    "    np.save(prefix + 'probe-flatten-R-%0.2f.npy' % R, flatten)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
